import os
import pandas as pd
import tushare as ts
import time
# import datetime
from datetime import datetime, date, timedelta
import warnings
import numpy as np
import talib as tb
import re
import __init__
from get_data import other
from funcat import *
import ts_his
import ths_vol
from get_data import Altas_db
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
import unittest
"""
1.数据获取
2.数据整合
3.循环策略
3.1策略
"""
start_time, today_time, _hour_= other.time_start()  # start_time,today_time,today_hour=时间('20200731', '20200820', '16:12')
ts.set_token('b31e0ac207a5a45e0f7503aff25bf6bd929b88fe1d017a034ee0d530')
pro = ts.pro_api()
time_zhou6 = int(pro.trade_cal(exchange='', start_date='20200101', end_date=today_time).is_open[-1:])




# _____________________________
def get_today_data():
    """
    1.数据获取
    :return:
    """
    csv_data=ts_his.get_today_ts()
    csv_data = csv_data[~csv_data.name.str.contains('ST')]  # 3洗掉ST
    csv_data = csv_data[~csv_data['open'].isin([0])]  # 4过滤开盘为0
    csv_data = csv_data[csv_data["trade"] < 45]  # 价格小于35
    csv_data = csv_data[csv_data["changepercent"] > -5]  # 5.删掉-3%以上的
    # csv_data=csv_data[(csv_data["amount"]>100000000)]
    # csv_data = csv_data[csv_data["turnoverratio"] > 1]  # 5.换手率大于2.5
    csv_data = csv_data.sort_values(by="turnoverratio", ascending=False)

    if time_zhou6 == 0:  # 周六==0,预测系数为1
        csv_data["volume_predict"] = csv_data["volume"]
        csv_data["turnoverratio_predict"] = csv_data["turnoverratio"]
    else:  # 平时进行预测
        csv_data = other.V_predict_data(csv_data)
        csv_data = csv_data[(csv_data["turnoverratio_predict"] > 2) | (csv_data["volume_predict"] > 18000000)]  # 换手率大于3
    csv_data.to_csv("db_temp/{}筛选数据.txt".format(today_time), encoding="utf-8")
    print("db_temp/{}筛选数据.txt".format(today_time))
    return csv_data
def merge_data(dtype="2"):
    """
    2.数据整合
    :return:
    """
    data_ma = ths_vol.today_volAndpri_read(day=today_time)
    # data_ma= other.re_read_df(data_ma)
    # _a_ = get_today_data()
    # if len(_a_) == 0:
    #     print("当日获取空集，todo")
    csv_data=ts_his.DmiWith_V_ma5()
    if not "turnoverratio" in csv_data.columns:
        df_1 = Altas_db._readdf("ts", "1.数据下载")
        csv_data=pd.merge(csv_data, df_1[["code","liutongliang","volume","settlement"]], how='inner', on='code')
        csv_data["volume_predict"] = csv_data["volume"]
        csv_data["turnoverratio"]=round(csv_data["volume_predict"]/csv_data["liutongliang"],2)

        cho1=str(csv_data[csv_data.code=="300224"].settlement)!=str(csv_data[csv_data.code=="300224"].c_)

        cho2=str(csv_data[csv_data.code == "300224"].settlement) != str(csv_data[csv_data.code == "300224"].c_1)
        print(str(csv_data[csv_data.code == "300224"].settlement)==str(csv_data[csv_data.code == "300224"].c_1))
        print(cho2,cho1)
        if cho1 and cho2 :
            print("价格出现问题1，请检查代码csv_data[csv_data.code==""]")

        #csv_data["turnoverratio_predict"] = csv_data["turnoverratio"]
    if time_zhou6 == 0:  # 周六==0,预测系数为1
        pass
        # csv_data["volume_predict"] = csv_data["v_"]*100
        # df_=Altas_db._readdf("ts","1.数据下载")
        # csv_data=pd.merge(csv_data, df_[["code","liutongliang"]], how='inner', on='code')
        # csv_data["turnoverratio"]=round(csv_data["v_"]/csv_data["liutongliang"],2)
        # #csv_data["turnoverratio_predict"] = csv_data["turnoverratio"]
    if time_zhou6 == 1:  # 平时进行预测
        if _hour_ > "15:30":
            pass
            # csv_data["volume_predict"] = csv_data["v_"] * 100
            # df_ = Altas_db._readdf("ts", "1.数据下载")
            # csv_data = pd.merge(csv_data, df_[["code", "liutongliang"]], how='inner', on='code')
            # csv_data["turnoverratio"] = round(csv_data["v_"] / csv_data["liutongliang"], 2)
        else:
            csv_data = other.V_predict_data(csv_data)
            csv_data = csv_data[(csv_data["turnoverratio_predict"] > 2) | (csv_data["volume_predict"] > 18000000)]  # 换手率大于3

    data_ = pd.merge(csv_data, data_ma, how='inner', on='code')

    # 2020.12.19增加第三种120不好，解决方法
    data_["vol_min"] = data_[["5日", "120日"]].min(axis=1)
    data_["vol_max"] = data_[["30日", "vol_min"]].max(axis=1)

    """
    5     |5      |30*  |30*      |120  |120
    30*   |120*   |5    |120      |30*  |5*
    120   |30     |120  |5        |5    |30
    第三种120不好，解决方法：和120比较，取大"""
    data_["vol_max2"] = data_[["120日", "vol_max"]].max(axis=1)
    data_ = data_[
        (data_["volume_predict"] > data_["vol_max2"] * 10000) | (data_["volume_predict"] > data_["120日"] * 2000000)]
    data_.to_csv("db_temp/{}符合vp@vol_max条件数据有{}项.txt".format(today_time, len(data_)), encoding="utf-8")
    if len(data_) > 1500:
        print("超涨了已经，寻找白马优势股票开始干，牛市来临")
    print(data_[data_.code=="300224"])
    data_ = data_[data_["volume_predict"] > data_["v_ma5"]]
    print("300224" in data_.code.tolist())
    # data_ = data_[data_["v_ma5"] >10000*data_["5日"]]
    data_.sort_values(by="turnoverratio", ascending=False)

    if dtype=="1":
        print(len(data_))
        celve(df_hb=data_)

    if dtype=="2":
        Altas_db._save_mongo_db(data_,"ts","3.5日小结")
        df_ = Altas_db._read_ths_volMax(dtype="pri")
        print(df_)
        #data_.to_csv("db_temp/3.5日小结.txt", encoding="utf-8")
def celve(df_hb):
    """
    3.循环策略
    :param df_hb:
    :return:
    """
    n = 0
    global funcat_time
    funcat_time, len_time = other.funcat_time()  # 20200403 ,防止时间内没数据
    len_time, time_0, time_1 = other.len_fangzhi(start_time, today_time)
    for i_code in df_hb.code:
        # if re.match(r'^(8).*', i_code):
        #     continue
        # if re.match(r'^(4).*', i_code):
        #     continue
        # 防搬，正则
        szcode = other.gu_zhengze_sz(i_code)
        len_time_sz = other.len_fangzhi_szcode(szcode, start_time, today_time)
        if len_time_sz <= len_time - 1:  # 防止出错
            # print(szcode,"___________________________")
            continue

        # print(i_code,"celve")
        _today_price = df_hb.loc[df_hb['code'] == i_code, 'trade']
        _today_high = df_hb.loc[df_hb['code'] == i_code, 'high']
        _today_open = df_hb.loc[df_hb['code'] == i_code, 'open']
        _today_low = df_hb.loc[df_hb['code'] == i_code, 'low']
        _today_settlement = df_hb.loc[df_hb['code'] == i_code, 'settlement']
        _today_v = df_hb.loc[df_hb['code'] == i_code, 'volume']
        _today_vp = df_hb.loc[df_hb['code'] == i_code, "volume_predict"]
        _today_tp = df_hb.loc[df_hb['code'] == i_code, "turnoverratio_predict"]

        if len((_today_price - _today_settlement).values) > 1:  # float64 2会出错，1才是对的,去掉读取不对的（很重要）
            continue
        global today_price, today_high, today_open, today_low, today_settlement, today_v, today_vp, today_tp

        today_price = _today_price.item()
        today_high = _today_high.item()
        today_open = _today_open.item()
        today_low = _today_low.item()
        today_settlement = _today_settlement.item()
        today_v = _today_v.item()
        today_vp = _today_vp.item()
        today_tp = _today_tp.item()
        # 周线判断

        """还可在周末进行变换， 60轴线/5 >每日"""
        try:
            df_w = ts.get_hist_data('{}'.format(i_code), ktype='W')[:130]  # 获取周k线数据
            df_w = df_w[::-1]  # 进行反转
            # df_w['30min_c_ma50'] = df_w.volume.rolling(window=120).mean()
            df_w['w_ma60'] = tb.MA(np.array(df_w.volume), timeperiod=60)
            df_w_shangzhou = (
                    df_w[len(df_w) - 2:len(df_w) - 1]["volume"] > df_w[len(df_w) - 2:len(df_w) - 1]['w_ma60']).item()
            df_w_benzhou = today_vp > (df_w[len(df_w) - 2:len(df_w) - 1]['w_ma60']).item() / 4

        except:
            df_w_shangzhou, df_w_benzhou = False, False

        if df_w_shangzhou or df_w_benzhou:  # 周线成交量大于60天均量线
            time.sleep(0.121)  # 2020.9.21增加
            n += 1
            print(n, "")
            xixi(code=i_code)
"""    ts_code    code        name      area     industry list_date dxw   renqi  \time  close_yestday          MA_5         MA_30        MA_120  \  H_10    L_10   MA_C_50       vol_min       vol_max      vol_max2 
0     000001.SZ  000001      平安银行   深圳       银行  19910403  NaN   102.0 \ fc时间20201225          18.04  7.791452e+07  8.574498e+07  1.278677e+08s \ 19.10   17.79   18.3974  7.791452e+07  8.574498e+07  1.278677e+08
1     000002.SZ  000002       万科A   深圳     全国地产  19910129  NaN   215.0 
"""
def xixi(code: str):
    """
    3.1测了测主函数
    :param code:
    :return:
    """
    S(code)
    T(funcat_time)


    # print(today_price)
    tr_today = MAX(MAX((today_high - today_low), ABS(CLOSE - today_high)), ABS(CLOSE - today_low))
    hd = today_high - HIGH
    ld = LOW - today_low
    TR = MAX(MAX(HIGH - LOW, ABS(HIGH - REF(CLOSE, 1))), ABS(LOW - REF(CLOSE, 1)))
    HD = HIGH - REF(HIGH, 1)
    LD = REF(LOW, 1) - LOW

    atr_today = (SUM(TR, 9) + tr_today) / 10
    atr_MtrAtr_4 = (atr_today / today_price > 0.04 and atr_today > tr_today) or COUNT(TR / STD(CLOSE, 13) > 1.3,
                                                                                      10) >= 2  # Mtr<ATR and ATR/price>0.04

    # __________________________________________

    # __________________________________________
    tr_toal_today_2 = (tr_today + TR)
    dmp_today = IF((HD > 0) & (HD > LD), HD, 0) + IF((hd > 0) & (hd > ld), hd, 0)
    dmm_today = IF((LD > 0) & (LD > HD), LD, 0) + IF((ld > 0) & (ld > hd), ld, 0)
    di1 = dmp_today * 100 / tr_toal_today_2
    di2 = dmm_today * 100 / tr_toal_today_2

    TR_2 = SUM(MAX(MAX(HIGH - LOW, ABS(HIGH - REF(CLOSE, 1))), ABS(LOW - REF(CLOSE, 1))), 2)
    DMP = SUM(IF((HD > 0) & (HD > LD), HD, 0), 2)
    DMM = SUM(IF((LD > 0) & (LD > HD), LD, 0), 2)
    DI1 = DMP * 100 / TR_2
    DI2 = DMM * 100 / TR_2

    k1, d1, j1 = KDJ(N=9, M1=3, M2=3)
    kk = k1 < d1 or COUNT(CROSS(k1, d1), 2) >= 1
    # (today_price-today_open)/today_price>0.035    and (today_price-today_low)/today_price<0.035
    x1 = (today_price - today_low) / today_price < 0.035
    x2 = (today_price - today_open) > (today_high - today_low) / 3
    # ——————————————————————————2020.9.8修改，关于整合开口布林带等策略
    r1, r2, r3 = RSI(N1=6, N2=12, N3=24)
    rsi_a = max((today_price - today_settlement), 0)
    rsi_b = abs((today_price - today_settlement))
    rsi_a1 = 5 * SMA(MAX(CLOSE - REF(CLOSE, 1), 0), 6, 1)
    rsi_b1 = 5 * SMA(ABS(CLOSE - REF(CLOSE, 1)), 6, 1)
    # RSI_today_a=(MAX((C-today_price)*-1,0)+5*SMA(MAX(C - REF(C, 1), 0), 6, 1))/6
    # RSI_today_b=(ABS(C-today_price)+5*SMA(ABS(CLOSE - REF(C, 1)), 6, 1))/6
    # RSI_today=RSI_today_a/RSI_today_b* 100
    RSI_today = (rsi_a + rsi_a1) / (rsi_b + rsi_b1) * 100
    RX = (r1 > 52 or r1[1] > 52 or r1[2] > 52)
    ##print(i_code,"RX_昨天",RX)

    # 2.tr,atr=ATR(M1=10)
    TR = MAX(MAX((HIGH - LOW), ABS(REF(CLOSE, 1) - HIGH)), ABS(REF(CLOSE, 1) - LOW))
    ATR = SUM(TR, 10) / 10
    #
    tr_today = MAX(MAX((today_high - today_low), ABS(CLOSE - today_high)), ABS(CLOSE - today_low))
    atr_today = (SUM(TR, 9) + tr_today) / 10
    #
    atr_bo = MAX(tr_today, atr_today) / today_price
    atr_b = atr_bo > 0.04

    TRX = max(TR, ATR) / CLOSE > 0.035 or max(TR[1], ATR[1]) / CLOSE[1] > 0.035 or max(TR[2], ATR[2]) / CLOSE[2] > 0.035
    ##print("TR_昨天",TRX)

    # 3.V-->V_t
    V_30 = SUM(V, 30) / 30
    V_60 = SUM(V, 60) / 60
    V_t = today_v > min(V_30, V_60)
    V_t1 = 5 * today_v / SUM(V, 5) > 1.5

    # 4.std
    std_yestday_list = []
    for i in range(0, 13):
        std_yestday_i = float(str(CLOSE[i]))
        std_yestday_list.append(std_yestday_i)
        if len(std_yestday_list) == 13:
            # print(std_yestday_list)
            std_today_list = std_yestday_list[0:12]
            std_today_list.append(today_price)
            std_yestday_1_list = std_yestday_list[1:13]
            std_yestday_1_list.append(float(str(CLOSE[13])))
            std_yestday_1 = np.std(std_yestday_1_list)
            # print(std_yestday1)
            std_yestday_2_list = std_yestday_list[2:13]
            std_yestday_2_list.append(float(str(CLOSE[13])))
            std_yestday_2_list.append(float(str(CLOSE[14])))
            std_yestday_2 = np.std(std_yestday_2_list)
            # print(std_yestday_2_list)
            # print(std_yestday_list)
            # print(std_today_list)
            # print("OK")

            # std_yestday_1=np.std(std_yestday_1_list)

            std_yestday = np.std(std_yestday_list)
            std_today = np.std(std_today_list)
            # print(std_yestday_1)
            # print(std_yestday,std_today)
    STDX = std_yestday_2 < std_yestday_1 or std_yestday_1 < std_yestday or std_yestday < std_today
    ##print("STD_昨天",STDnonX)

    HV_1 = (COUNT(H > REF(HHV(H, 10), 1), 3) >= 1 or today_high > HHV(H, 10))
    ##print(HV_1)
    ##print( COUNT(TR/CLOSE>0.06,19)>=2)

    # 5.一目均衡云
    zk = (HHV(HIGH, 7) + LLV(LOW, 7)) / 2
    zd = (HHV(HIGH, 20) + LLV(LOW, 20)) / 2
    # print("LLLLLLLLL",V > min(V_30, V_60) or 5 * V / SUM(V, 5) > 1.5)

    if (V > min(V_30, V_60) or 5 * V / SUM(V, 5) > 1.5) and (V_t or V_t1) and TRX and COUNT(TR / CLOSE > 0.06,
                                                                                            19) >= 2 and RX and HV_1 and STDX and 0.97 < CLOSE / \
            CLOSE[1] < 1.075 and today_price > (H + L) / 2 and (today_price - today_open) > (
            today_high - today_low) / 3:
        # print(i_code_temp,today_price,RSI_today>r1,tr_today,atr_today,V_t,V_t1,min(V_30,V_60),SUM(V,5))
        print(code, std_yestday, std_today, max(TR, ATR), max(TR[1], ATR[1]), max(TR[2], ATR[2]),
              max(today_high, HHV(H, 10)) / std_today, std_today, V > min(V_30, V_60), 5 * V / SUM(V, 5) > 1.5, V,
              SUM(V, 5) / 5, today_price / CLOSE - 1)
        # list_collect_bolling.append(
        #     [i_code, today_vp / (float(str(100 * V))), today_vp / (float(str(100 * MA(V, 120)))),
        #      "{}-{}".format(today_time, today_hour), time_t, (today_price - today_low) / today_price, today_price,
        #      str(zk[0]), str(zd[0])])
    # ——————————————————————————
    """
    if MA(CLOSE, 50) * 1.2 > today_low > MA(CLOSE,
                                            50) and atr_MtrAtr_4 and today_price / today_settlement < 1.07 and today_price / LLV(
            C, 20) < 1.20:  # and C<30
        if ((today_high > H and today_low < L) or today_high > H[
            1] > HIGH) and today_price > CLOSE and today_price > today_open:
            list_collect.append([i_code, today_vp / (float(str(100 * V))), today_vp / (float(str(100 * MA(V, 120)))),
                                 "{}-{}".format(today_time, today_hour), time_t,
                                 (today_price - today_low) / today_price, today_price, str(zk[0]), str(zd[0])])
            print([i_code, "和昨天相比", today_vp / (float(str(100 * V))), "和MA120比",
                   today_vp / (float(str(100 * MA(V, 120)))), "{}-{}".format(today_time, today_hour), time_t,
                   (today_price - today_low) / today_price, today_price], atr_today / today_price,
                  today_price / today_settlement)
    if CLOSE < MA(CLOSE, 50) < 30:
        if today_high > max(H[1], HIGH) and today_price > CLOSE and today_price / today_settlement > 1.03:
            print("--------------小雨ma50", i_code)
            list_collect_2.append([i_code, today_vp / (float(str(100 * V))), today_vp / (float(str(100 * MA(V, 120)))),
                                   "{}-{}".format(today_time, today_hour), time_t,
                                   (today_price - today_low) / today_price, today_price, str(zk[0]), str(zd[0])])
        if today_high > H[2] > max(H[1], HIGH) and today_price > CLOSE and today_price / today_settlement > 1.03:
            print("--------------小雨ma50大于4天前=========请注意", i_code)
            list_collect_2.append([i_code, today_vp / (float(str(100 * V))), today_vp / (float(str(100 * MA(V, 120)))),
                                   "{}-{}".format(today_time, today_hour), time_t,
                                   (today_price - today_low) / today_price, today_price, str(zk[0]), str(zd[0])])

"""

if __name__ == '__main__':
    merge_data(dtype="2")
    # list_max_name = Altas_db._read_ths_volMax(dtype="pri")
    # df_=Altas_db._readdf("ths_vol",list_max_name)
    # print(df_)
